package service;

import dao.JPADAOFactory;
import dao.exception.AlreadyEntityExistsException;
import dao.exception.NotEntityFoundException;
import entity.TipoDeContratacion;
import javax.persistence.EntityManager;
import service.exception.DuplicateObjectException;
import service.exception.ObjectNotFoundException;
import valueObject.TipoDeContratacionVO;

/**
 *
 * @author Juan Sebastian Hernández Serrato
 */
public class TipoDeContratacionService extends AbstractService<TipoDeContratacionVO, TipoDeContratacion> {

    @Override
    public int create(TipoDeContratacionVO valueObject, EntityManager em) throws DuplicateObjectException{
        TipoDeContratacion tipoDeContratacion = valueObject.toEntity();
        try {
            JPADAOFactory.getInstance().getTipoDeContratacionDAO().create(tipoDeContratacion, em);
            return tipoDeContratacion.getId();
        } catch (AlreadyEntityExistsException ex) {
            throw new DuplicateObjectException();
        }
    }

    @Override
    public TipoDeContratacionVO findById(int id, EntityManager em) throws ObjectNotFoundException {
        try {
            return JPADAOFactory.getInstance().getTipoDeContratacionDAO().findById(id, em).toValueObject();
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public void update(TipoDeContratacionVO valueObject, EntityManager em) throws ObjectNotFoundException {
        try {
            TipoDeContratacion tipoDeContratacion = JPADAOFactory.getInstance().getTipoDeContratacionDAO().findById(valueObject.getId(), em);
            tipoDeContratacion = valueObject.toEntity();
            JPADAOFactory.getInstance().getTipoDeContratacionDAO().update(tipoDeContratacion, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public void delete(int id, EntityManager em) throws ObjectNotFoundException {
        try {
            TipoDeContratacion tipoDeContratacion = JPADAOFactory.getInstance().getTipoDeContratacionDAO().findById(id, em);
            JPADAOFactory.getInstance().getTipoDeContratacionDAO().delete(tipoDeContratacion, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

}
